<?php


/**
 * 解析配置文件信息
 */


/**
 * @param null $name
 * @return array|bool|mixed|null
 */
function config($name = null)
{
    static $config = [];

    if (empty($config)) {

        if (is_file('./Config.ini')) {
            $tools = false;
            $config = parse_ini_file('./Config.ini', true);
        } elseif (is_file('./tools/Config.ini')) {
            $tools = true;
            $config = parse_ini_file('./tools/Config.ini', true);
        } else {
            $tools = false;
            dump('配置文件路径错误');
        }

        $replace = function ($value) {
            return str_replace('./tools/', './', $value);
        };

        if ($tools === false && $config) {

            foreach ($config as $key => $item) {

                if (is_array($item)) {
                    foreach ($item as &$value) {
                        $value = $replace($value);
                    }
                    unset($value);
                } else {
                    $item = $replace($item);
                }

                $config[$key] = $item;
            }
        }
    }


    if (null !== $name) {
        if (preg_match('/\./', $name)) {
            $variable = explode('.', $name);
            return isset($config[$variable[0]][$variable[1]]) ? $config[$variable[0]][$variable[1]] : '';
        } else {
            return isset($config[$name]) ? $config[$name] : null;
        }
    }

    return $config;
}
